package com.android.kuai.zhuan.adapter;

import android.content.Context;
import android.graphics.Color;
import android.media.Image;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.kuai.zhuan.R;
import com.android.kuai.zhuan.bean.AppListBean;
import com.android.kuai.zhuan.utils.CommonUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class AppListAdapter extends BaseAdapter {
    List<AppListBean.DataBean> list;
    Context mContext;
    RequestOptions options;

    public AppListAdapter(Context context, List<AppListBean.DataBean> appList) {
        mContext = context;
        list = appList;
        RoundedCorners corners = new RoundedCorners(24);
        options = RequestOptions.bitmapTransform(corners);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_gridview, null, false);
            holder = new ViewHolder();
            holder.appIcon = convertView.findViewById(R.id.app_cion);
            holder.appName = convertView.findViewById(R.id.app_name);
            holder.parent = convertView.findViewById(R.id.gridview_itme_layout);
            holder.installStatus = convertView.findViewById(R.id.install_staus);
            holder.badge = new QBadgeView(mContext).bindTarget(holder.parent)
                    .setBadgeTextColor(Color.BLACK)
                    .setBadgeBackgroundColor(ContextCompat.getColor(mContext, R.color.toolBar_bg))
                    .stroke(Color.WHITE, 1, true)
                    .setBadgePadding(5, true)
                    .setGravityOffset(5, 0, true);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.appName.setText(list.get(position).getName());
        Glide.with(mContext).load(list.get(position).getIcon()).apply(options).into(holder.appIcon);
        int times = list.get(position).getTimes();

        if(!CommonUtils.appIsAvilible(mContext, list.get(position).getPackageX())) {
            holder.installStatus.setVisibility(View.VISIBLE);
            holder.badge.setBadgeNumber(0);
        } else {
            holder.installStatus.setVisibility(View.GONE);
            holder.badge.setBadgeNumber(times);
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView appIcon;
        TextView appName;
        TextView installStatus;
        View parent;
        Badge badge;
    }

    public void setData(List<AppListBean.DataBean> appList) {
        this.list = appList;
        notifyDataSetChanged();
    }
}
